package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Consumer;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    //新增套餐
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmeal.setStatus(StatusConstant.ENABLE);

//        setmeal.setCreateTime(LocalDateTime.now());
//        setmeal.setUpdateTime(LocalDateTime.now());
//        Long currentId = BaseContext.getCurrentId();
//        setmeal.setCreateUser(currentId);
//        setmeal.setUpdateUser(currentId);
        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long setmealId = setmeal.getId();
//        setmealDTO.setCreateUser(BaseContext.getCurrentId());
//        setUpdateUser(BaseContext.getCurrentId());
        if (setmealDishes != null && setmealDishes.size()!=0){
            //setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }

            setmealDishMapper.insert(setmealDishes);
        }




        /*{
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
//        Dish dish = new Dish();
        SetmealDish setmealDish1 = new SetmealDish();
        Setmeal setmeal1 = new Setmeal();
        BeanUtils.copyProperties(setmeal,setmealDishes);
        BeanUtils.copyProperties(setmeal,setmeal1);
        setmeal1.setCreateUser(BaseContext.getCurrentId());
        setmeal1.setUpdateUser(BaseContext.getCurrentId());
        setmeal1.setCreateTime(LocalDateTime.now());
        setmeal1.setUpdateTime(LocalDateTime.now());
        setmealDishes.forEach(setmealDish -> setmealDish1.setSetmealId(setmeal.getId()));
        setmealDishes.forEach(setmealDish -> setmealDish1.setDishId(setmeal.getId()));
        setmealDish1.setDishId(dishId);
        setmealDish1.setSetmealId(setmealId1);
        setmealMapper.insert(setmeal);

        Long setmealId = setmeal.getId();//获取主键id 帮助后续的关联id
        setmealDishes.setSetmealId(setmealId);
        //Long dishId = dish.getId();
        //setmealDish.setDishId(dishId);//添加套餐内关联的菜品id
        setmealDishMapper.insert(setmealDishes);//添加到菜品套餐里
        //Long dishId1 = setmealDish.getDishId();
//        Integer status = setmeal.getStatus();
//        if (status == 1){

       // }
    }*/
    }

    //根据id查询套餐
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal selectById = setmealMapper.selectById(id);
        BeanUtils.copyProperties(selectById,setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectById(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    //分页查询
    @Override
    public PageResult select(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
         Page<SetmealVO> select = setmealDishMapper.select(setmealPageQueryDTO);
        return new PageResult(select.getTotal(),select.getResult());
    }

    //删除套餐
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //进行判断套餐是否还在出售
        for (Long id : ids) {
            //利用id查询套餐是否在出售状态
            Setmeal setmeals = setmealMapper.selectById(id);
            if (setmeals.getStatus() != 1) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids) {
            setmealMapper.delete(id);
            setmealDishMapper.delete(id);
        }
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //进行拷贝
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改数据
        setmealMapper.update(setmeal);
        //删除套餐关系原有的数据
        setmealDishMapper.delete(setmealDTO.getId());
        //重新添加套餐关系表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long setmealId = setmealDTO.getId();

        if (setmealDishes != null&&setmealDishes.size() >0){
          /*  setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));*/
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealDishMapper.insert(setmealDishes);
        }


    }

    @Override
    public void startOrStop(Integer status, Long id) {
        setmealMapper.startOrStop(status,id);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
